#!/usr/bin/perl -w  # useful for syntax highlighting!
package clreqd_modules;

use strict;
use cldb;
use Config;
use clnet qw(
		CLNET2_unassign_network_ips
);

sub unconfig_app_ips {
my %ARGS=@_;
my $rc;

	my $APP=$ARGS{APPLICATION};

	#################################################################
	# Get the details from the startscript and starttimeout		#
	#################################################################

	my $CLDB=new cldb("$::ENV{TRUECL_CFG}/config.db");
	if(!defined($CLDB)) {
		return "ERROR - Unable to config to 'config' database.";
	}
	my $res=$CLDB->get_rows("SELECT * FROM application_ips WHERE name = '$APP';");
	$CLDB->disconnect; $CLDB=undef;

	if(!defined($res->[0]->{name})) {
		::msg("No IP's configured for application '$APP'.");
		return "OK 0";
	}
	#################################################################
	# Generate a list of networks for address assignment.		#
	#################################################################

	my @nets=();
	push @nets,$_->{network} foreach(@$res);

	my @R=();
	my $cnet;
	for $cnet (@nets) {
		::msg("Unassigning network ips for network '$cnet'.");
		my ($rc,$err)=CLNET2_unassign_network_ips(
						network=>$cnet,
						results=>\@R,
						appname=>$APP);
		if($rc==0) {
			push @R,"ERROR $err" if defined($err);
			return "OK 1\n".join("\n",@R);
		}
	}
	return "OK 0\n".join("\n",@R);
}

1;
